package com.wltjack.common.reponse;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @ClassName Res
 * @Description http返回对像
 * @Author kangliang
 * @Date 2021/03/01 13:34
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(description = "响应信息主体")
public class Res<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回标记：成功标记=0，失败标记=1")
    private int code;

    @ApiModelProperty(value = "返回信息")
    private String msg;

    @ApiModelProperty(value = "数据")
    private T data;

    public static <T> Res<T> ok(T data) {
        return Res.buildResult(data, ResponseConstants.SUCCESS, "成功");
    }

    public static <T> Res<T> ok(T data, String msg) {
        return Res.buildResult(data, ResponseConstants.SUCCESS, msg);
    }

    public static <T> Res<T> failed(T data) {
        return Res.buildResult(data, ResponseConstants.FAIL, "失败");
    }

    public static <T> Res<T> failed(T data, String msg) {
        return Res.buildResult(data, ResponseConstants.FAIL, msg);
    }

    public static <T> Res<T> failed(T data, int code, String msg) {
        return Res.buildResult(data, code, msg);
    }

    /**
     * 功能描述: 构造响应结果
     *
     * @param data
     * @param code
     * @param msg
     * @return: Res<T>
     * @Author: kangliang
     * @Date: 2021-03-01 13:39
     */
    private static <T> Res<T> buildResult(T data, int code, String msg) {
        Res<T> apiResult = new Res<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
